package com.lc.hongtai.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.activity.InvestmentActivity;
import com.lc.hongtai.conn.JsonIndex;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/12.
 */
public class HomeListRecyAdapter extends RecyclerView.Adapter<HomeListRecyAdapter.ViewHolder> {

    private Context context;
    private List<JsonIndex.Info.Borrow_list> list = new ArrayList<>();

    public HomeListRecyAdapter(Context context, List<JsonIndex.Info.Borrow_list> list) {
        this.context = context;
        this.list = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        @Bind(R.id.tv_home_list_title)
        TextView tvRate;
        @Bind(R.id.tv_home_list_desc)
        TextView tvTime;

        @Bind(R.id.tv_sumMoney)
        TextView tvSumMoney;

        public String str;
        public int a;

        @Bind(R.id.view_lines)
        View view_lines;
        @Bind(R.id.tv_progress)
        TextView tv_progress;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //  View view = LayoutInflater.from(context).inflate(R.layout.item_home_list, null);
        View view = MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(context).inflate(R.layout.item_home_list, null));

        HomeListRecyAdapter.ViewHolder vh = new HomeListRecyAdapter.ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        if (list.size() - 1 == position) {

            holder.view_lines.setVisibility(View.GONE);
        } else {

            holder.view_lines.setVisibility(View.VISIBLE);

        }

        if (list.size() == 0) {

        } else {

            NumberFormat nfNumber = NumberFormat.getNumberInstance();
            holder.tvRate.setText(list.get(position).borrow_min + "元");
            String str_duration = list.get(position).borrow_duration;

            holder.tvTime.setText(list.get(position).borrow_duration + list.get(position).borrow_duration_cn);


            holder.tvSumMoney.setText(list.get(position).borrow_interest_rate);

            final Drawable indicator = context.getResources().getDrawable(
                    R.mipmap.jindu4);
            Rect bounds = new Rect(0, 0, indicator.getIntrinsicWidth(),
                    indicator.getIntrinsicHeight());
            indicator.setBounds(bounds);

            holder.tv_progress.setText(list.get(position).progress);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.isEmpty() | list.size() == 0) {

                    } else {
                        Intent intent = new Intent();
                        //   intent.putExtra("id", list.get(position).id);
                        intent.putExtra("id", list.get(position).id);
                        intent.setClass(context, InvestmentActivity.class);
                        context.startActivity(intent);
                    }


                }
            });
        }

    }


    @Override
    public int getItemCount() {
        return list.size();
    }


}
